Chapter 8: Loop Structures and Booleans
Loops and shit You already know that the Python for statement provides a kind of loop, and it allows us to iterate through a sequence of values, using the general formula for in : #I dont like this formatting But what if we want to write a program that can compute the average of a series of numbers entered by the user? I dont feel like typing that whole paragraph in the book out, so lets just say that the design patterns can get complex, so we need to learn more things. Indefinite Loops iterates until a certain condition is met. No guarantee ahead of time how many loops. Use a while statement While : Here is a boolean expression. Notice that the condition is always tested at the top of the loop, before the loop body is executed. This is called a pre-test loop, and the loop will not execute if the condition is initially false, meaning the body will not execute at all. Example: i=0 #Notice that we have to initialize i before the loop in the while loop While i<=10: print(i) i=i+1 This code will have the same output as if we had written a for loop like: for i in range(11): print(i) WHAT IS WE DUN GOOF AND FORGET TO INCREMANT THE i, MEANING THE CODE LOOKS LIKE i=o while i<=10: print(i) THEN YOU WILL PRINT 0 INFINITELY this is bad usually break loop by pressing -c, or if the forces of chaos are too powerful you have to press ctrl alt delete Interactive loops One good use of indefinite loops is to write interactive loops. They allow the user to repeat certain portions of a program on demand. Example: def main(): total=0.0 count=0 moredata="yes" while moredata[] "y": x=float(""Enter a number") total=count+x count=count+1 moredata=input("Do you have more numbers (yes or no)?") print("\n The average of the numbers is",total/count") main() run it yourselves to see the results you lazy bastards Sentinel Loops a better solution to the number-averaging problem is to employ a pattern commonly known as a sentinel loop These continue to process data until reaching a special value that signals the end. This value is called a sentinel. This can be any value, the only restriction is that it has to be distringuishable from actual data values. The sentinel is not processed as part of the data. General pattern: get the first data item while item is not the sentinel process the item get the next data item Notice the pattern avoids processing the sentinel item The first item is retrieved before the loop starts, this is sometimes called the priming read, as it gets the process started. if the first item is the snetinel, the loop immediately terminates and no data is processed. Sometimes its a good idea to make the sentinel an empty string(" ") File Loops What if you make a typo when typing out values to a program,like an average program, you dont want to have to type that all again do you? Type them all to a file and put in some magic code. infile=open(fileName,'r') This is usually used for data processing the readline() function can also be useful. it gets the next line from a file as a string general pattern: line=infile.readline() while line !=" ": #process line line=infile.readline() Other things What if you want a value but the dumbass user submits something else. The program will repromt until the user enters a valid value. This process is called input validation Post test loop: When a condition test comes after the loop Loop and a half: when an exit loop is in the middle of the loop body Event Loop: When the sentinel is an event(think in graphic programs)